package com.eleventwelve.iris.persistence.evolve;

import java.util.ArrayList;
import java.util.List;

import org.opentides.bean.user.UserCredential;
import org.opentides.bean.user.UserGroup;
import org.opentides.persistence.evolve.DBEvolve;
import org.opentides.persistence.evolve.Evolver;
import org.opentides.service.UserGroupService;
import org.opentides.service.UserService;

import com.eleventwelve.iris.entity.IrisUser;

public class DBEvolve002 extends Evolver implements DBEvolve {
	
	private UserGroupService userGroupService;
	
	private UserService userService;

	private void addNewAuthzForUsers() {
		//1 - Super User
		executeSqlUpdate("INSERT INTO authorities (`AUTHORITY`, `USERGROUP_ID`)"
				+ " VALUES ('ACCESS_USER_ROLE_TAB', 1), "
				+ " ('ACCESS_REPORTS_TAB', 1), "
				+ " ('ACCESS_ADMINS_TAB', 1);");
		
		//2 - Staff
		executeSqlUpdate("INSERT INTO authorities (`AUTHORITY`, `USERGROUP_ID`)"
				+ " VALUES ('ACCESS_DASHBOARD_TAB', 2), "
				+ " ('ACCESS_PATIENTS_TAB', 2), "
				+ " ('ACCESS_PROCEDURES_TAB', 2), "
				+ " ('ACCESS_PROGRAMS_TAB', 2), "
				+ " ('ACCESS_DISEASES_TAB', 2), "
				+ " ('ACCESS_ALLERGENS_TAB', 2), "
				+ " ('ACCESS_MEDICINES_TAB', 2), "
				+ " ('ACCESS_REPORTS_TAB', 1), "
				+ " ('ACCESS_ADMINS_TAB', 2);");
		
		//3 - Guest
		executeSqlUpdate("INSERT INTO authorities (`AUTHORITY`, `USERGROUP_ID`)"
				+ " VALUES ('GUEST_ACCESS', 3);");
	}
	
	private void addNewUsers() {
		List<String> roleNames = new ArrayList<String>();
		roleNames.add("ACCESS_ALL");
		
		UserGroup staffGroup = new UserGroup();
		staffGroup.setName("Staff");
		staffGroup.setDescription("Can Manage data of the patients.");
		staffGroup.setRoleNames(roleNames);
		
		userGroupService.save(staffGroup);
		
		UserGroup guestGroup = new UserGroup();
		guestGroup.setName("Guest");
		guestGroup.setDescription("Can Register for a new patient.");
		guestGroup.setRoleNames(roleNames);
		
		userGroupService.save(guestGroup);
		
		IrisUser staff = new IrisUser();
		UserCredential staffuser = new UserCredential();
		
		staffuser.setUsername("staff");
		staffuser.setPassword("eleventwelve");
		staffuser.setEnabled(true);
		staffuser.setUser(staff);
		
		staff.setCredential(staffuser);
		staff.setFirstName("IrisStaff");
		staff.setLastName("User");
		staff.setEmailAddress("iris_staff@eleventwelve.com");
		staff.addGroup(staffGroup);
		
		this.userService.save(staff);
		
		IrisUser guest = new IrisUser();
		UserCredential guestuser = new UserCredential();
		
		guestuser.setUsername("guest");
		guestuser.setPassword("eleventwelve");
		guestuser.setEnabled(true);
		guestuser.setUser(guest);
		
		guest.setCredential(guestuser);
		guest.setFirstName("Guest");
		guest.setLastName("User");
		guest.setEmailAddress("guest@eleventwelve.com");
		guest.addGroup(guestGroup);
		
		this.userService.save(guest);
	}
	
	public void setUserGroupService(UserGroupService userGroupService) {
		this.userGroupService = userGroupService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	
	@Override
	public void execute() {
		addNewUsers();
		addNewAuthzForUsers();
	}

	@Override
	public String getDescription() {
		return new String("DBEvolve to set auths for users and add new user group.");
	}

	@Override
	public int getVersion() {
		return 2;
	}

}
